﻿import QtQuick 2.12

Rectangle {
    function appendTxt(str) {
        // 判断是否在底部
        // var atBottom = Math.abs(flick.contentY + flick.height - flick.contentHeight) < 2

        txtEdit.append(str)

        // 只显示50行内容
        var lines = txtEdit.text.split('\n')
        if (lines.length > 50) {
            lines = lines.slice(lines.length - 50)
            txtEdit.text = lines.join('\n')
        }

        // 只有在底部时才自动滚动
        // if (atBottom) {
        //     flick.contentY = flick.contentHeight - flick.height
        // }
    }

    border.color: "black"
    border.width: 1

    Flickable {
        id: flick
        anchors.fill: parent
        anchors.topMargin: 10
        anchors.leftMargin: 10
        contentHeight: txtEdit.height
        clip: true

        TextEdit {
            id: txtEdit
            width: flick.width
            height: Math.max(flick.height, contentHeight)
            font.pixelSize: 18
            text: ""
            textFormat: TextEdit.PlainText
            wrapMode: TextEdit.NoWrap
            readOnly: true // 如只做显示
        }
    }
}
